﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using EIS.ClientUtility;
using EIS.DatabaseHelper;
using EIS.Obj;
using EIS.ServerAdapter;

namespace Ext.Connector.EIS
{
    public class EISFSAP0002:SAPConnector
    {
        public EISFSAP0002(string functionID)
        {
            this.FunctionID = functionID;
        }

        private void Connect()
        {
            string url = GlobalVariable.Instance.ServerRootURL;

            url = url + @"functionConfig/" + FunctionID + @"/" + GlobalVariable.Instance.CurrentToken + ".xml";

            DataSet dsConfig = RestAdapter.HttpGet(url);

            if ((dsConfig == null) || (dsConfig.Tables.Count == 0)) return;
            var dr = dsConfig.Tables[0].Rows[0];
            DataSet dsSaveSAPConnectionInfo = new DBHandler().SelectSimple(EISConstant.SAPConnectionTableName);

            //TODO if system type == SAPPassword , SAPSSO
            foreach (DataRow drSAPConnectionInfo in dsSaveSAPConnectionInfo.Tables[0].Rows)
            {
                if (
                    (drSAPConnectionInfo["UserID"].Equals(ClientConfig.Instance.LogonInfo.UserName)) &&
                    (drSAPConnectionInfo["SAPClient"].ToString() == dr["client"].ToString()) &&
                    (drSAPConnectionInfo["SAPID"].ToString() == dr["systemID"].ToString()) 
                    )
                {
                    this.Connect(dr["systemAddress"].ToString(), dr["instanceNo"].ToString(), dr["client"].ToString(),
                                 drSAPConnectionInfo["SAPUser"].ToString(),
                                 Decode(drSAPConnectionInfo["SAPPassword"].ToString()), "EN");
                }
            }
        }

        private string Decode(string encodedData)
        {

            byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);

            string returnValue = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

            return returnValue;

        }
        public override void Execute(object args)
        {
            Connect();
            var dataRow = (DataRowView)args;

            string orderID = dataRow["DOC_NUMBER"].ToString();

            if ( orderID == String.Empty)
            {
               
                ShowErrorMessageBox.Show("Error in Getting Sales Order ID");
            }
            else
            {
                if (r3Connection != null)
                    SAPSD.OpenSalesOrderByIDinSAP(orderID,this.r3Connection);
            }
        }

/*        private string GetFieldValue(List<KeyValuePair<string, string>> list, string fieldName)
        {
            var listIn = list;
            string orderID = String.Empty;
            foreach (var keyValuePair in listIn)
            {
                if (keyValuePair.Key.ToUpper() == fieldName.ToUpper())
                {
                    return keyValuePair.Value;
                }
            }
            return String.Empty;
        }*/
    }
}
